package com.hc.appInfoActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hc.model.ChartModel;
import com.hc.util.EquUtil;
import com.hc.util.fillData.AppInfoBean;
import com.hc.util.fillData.FillWifiData;

public class AppInfoService {
	
	

	public static List<AppInfoBean> appInfoBeans =  new ArrayList<AppInfoBean>();
	
	public List<AppInfoBean> getData(){
		
		Map<String,List<AppInfoBean>> data = 	fillData();
		List<AppInfoBean> list =  new ArrayList<AppInfoBean>();
		for(Object obj:data.keySet()){
			List<AppInfoBean> beans  = data.get(obj);
			
			for(AppInfoBean bean:beans){
				list.add(bean);
			}
			
		}
		updataList(list);
		
//		//对集合进行排序
//		ComparatorApp compara = new ComparatorApp();
//		Collections.sort(appInfoBeans, compara);
		return appInfoBeans;
	}
	private void updataList(List<AppInfoBean> list) {

			if(appInfoBeans.size()==0){
					
					appInfoBeans.addAll(list);
					
				}else{
					
				
						for(AppInfoBean bean2:list){
							
							boolean state = true;
							
							//每次使用appInfoBeans时，进行加锁，防止其他地方修改
							synchronized (appInfoBeans) {
								for(int i = 0,size = appInfoBeans.size(); i<size;i++){
									
									if((appInfoBeans.get(i).getWifiMac()+appInfoBeans.get(i).getChain()).equals(bean2.getWifiMac()+bean2.getChain()))
									{
										appInfoBeans.set(i,bean2);
										state = false;
									}
								
							  }
								  if(state){
									  appInfoBeans.add(bean2);
									  
								  }
							}
					}
				}
			

			
	}
	private  Map<String,List<AppInfoBean>> fillData() {
		List<String>	filePathList = new ArrayList<String>();
		 Map<String,List<AppInfoBean>> data = new HashMap<String, List<AppInfoBean>>();
		//加锁防止 ，其他地方修改集合     
		synchronized (ChartModel.filePathList) {
			filePathList.addAll(ChartModel.filePathList);
		}
		FillWifiData.wifimACList.clear();
		for(String path:filePathList){
			//将路径从"——"截取
			String[] b = path.split(EquUtil.MYFILEPATH+"/");
			String[] s =  b[1].split("_");
	
			int num =Integer.parseInt(s[0]);
			String	chainStr = ChartModel.routeArray[num-1]+"";
			
			try {
				data.put(s[0], FillWifiData.fillWifiInfo(path, chainStr));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return data;
}
}
